package com.jss.lanouhn.fristapp.fragments.homeChirdFragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.adapters.HomeBigAdapter;
import com.jss.lanouhn.fristapp.modles.homeModles.HomeRecyclerView;
import com.ybao.pullrefreshview.layout.BaseFooterView;
import com.ybao.pullrefreshview.layout.BaseHeaderView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/8/25.
 */
public class BaseFragment extends Fragment implements BaseHeaderView.OnRefreshListener, BaseFooterView.OnLoadListener {
    private RecyclerView recyclerView;
    BaseHeaderView headerView;
    BaseFooterView footerView;
    private HomeBigAdapter adapter;
    private String urls = "http://api.liwushuo.com/v2/channels/10/items_v2?gender=1&generation=2&limit=20&offset=";
    private int offset = 0;
    private List<HomeRecyclerView.DataBean.ItemsBean> listRecyview = new ArrayList<>();
    private List<HomeRecyclerView.DataBean.ItemsBean> tempListRecyview = new ArrayList<>();
//    PtrClassicFrameLayout ptrClassicFrameLayout;
//    RecyclerAdapterWithHF madapter = new RecyclerAdapterWithHF(adapter);

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.base_home_fragment, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerView = (RecyclerView) getView().findViewById(R.id.rv_home_base);
//        ptrClassicFrameLayout = (PtrClassicFrameLayout) getView().findViewById(R.id.refresh_recycler_view);
        headerView = (BaseHeaderView) getView().findViewById(R.id.header);
        footerView = (BaseFooterView) getView().findViewById(R.id.footer);
        headerView.setOnRefreshListener(this);
        footerView.setOnLoadListener(this);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(manager);
        initData();
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(urls + offset)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(2);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        HomeRecyclerView homeRecyclerView = gson.fromJson(result, HomeRecyclerView.class);
                        int code = homeRecyclerView.getCode();
                        if (code == 200) {
                            tempListRecyview = homeRecyclerView.getData().getItems();
                            if (tempListRecyview.size() > 0) {
                                handler.sendEmptyMessage(1);
                            } else {
                                handler.sendEmptyMessage(2);
                            }
                        } else {
                            handler.sendEmptyMessage(3);
                        }
                    }
                });

            }
        }).start();
    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    if (offset == 0) {
                        listRecyview.clear();
                        listRecyview.addAll(tempListRecyview);
                        adapter=new HomeBigAdapter(getContext(),listRecyview);
                        recyclerView.setAdapter(adapter);
                        headerView.stopRefresh();

                    }else {
                        listRecyview.addAll(tempListRecyview);
                        adapter.notifyDataSetChanged();
                        footerView.stopLoad();

                    }
                    tempListRecyview.clear();

                    break;

                case 2:
                    Toast.makeText(getContext(), "请求网络失败", Toast.LENGTH_SHORT).show();
                    break;
                case 3:
                    footerView.stopLoad();
                    Toast.makeText(getContext(), "已经是最后一页了", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };



    @Override
    public void onRefresh(BaseHeaderView baseHeaderView) {
        offset = 0;
        initData();



    }

    @Override
    public void onLoad(BaseFooterView baseFooterView) {

        offset += 20;
        initData();

    }

}
